package com.dream.testtubebaby.adapter;

import java.util.List;

import android.content.Context;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.dream.testtubebaby.R;
import com.dream.testtubebaby.data.bbs.BbsNode;

public class BbsTopicSearchAdapter extends BaseAdapter {
	List<BbsNode> mDatas;
	Context context;

	public BbsTopicSearchAdapter(Context context, List<BbsNode> items) {
		this.context = context;
		this.mDatas = items;
	}

	public void setDatas(List<BbsNode> mDatas) {
		this.mDatas.clear();
		this.mDatas.addAll(mDatas);
	}

	public void addAll(List<BbsNode> mDatas) {
		this.mDatas.addAll(mDatas);
	}

	@Override
	public int getCount() {
		return mDatas.size();
	}

	@Override
	public Object getItem(int position) {
		return mDatas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		LayoutInflater layoutInflater = LayoutInflater.from(context);
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = layoutInflater.inflate(R.layout.bbs_list_item_layout,
					null);
			viewHolder.bbs_item_title_tv = (TextView) convertView
					.findViewById(R.id.bbs_item_title_tv);
			viewHolder.bbs_item_name_tv = (TextView) convertView
					.findViewById(R.id.bbs_item_name_tv);
			viewHolder.bbs_item_lookup_tv = (TextView) convertView
					.findViewById(R.id.bbs_item_lookup_tv);
			viewHolder.bbs_item_comment_tv = (TextView) convertView
					.findViewById(R.id.bbs_item_comment_tv);
			viewHolder.bbs_item_time_tv = (TextView) convertView
					.findViewById(R.id.bbs_item_time_tv);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		viewHolder.bbs_item_title_tv.setText(mDatas.get(position).getTitle());
		viewHolder.bbs_item_name_tv.setText(mDatas.get(position)
				.getPostUserName());
		viewHolder.bbs_item_lookup_tv.setText(String.valueOf(mDatas.get(
				position).getViewCount())
				+ context.getResources().getString(R.string.bbs_lookup_string));
		viewHolder.bbs_item_comment_tv
				.setText(String.valueOf(mDatas.get(position).getReplyCount())
						+ context.getResources().getString(
								R.string.bbs_comment_string));
		CharSequence time_string = DateUtils.getRelativeTimeSpanString(mDatas.get(position).getPostDate(),
                System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS,
                DateUtils.FORMAT_ABBREV_RELATIVE);
		 viewHolder.bbs_item_time_tv.setText(time_string);
		return convertView;
	}

	class ViewHolder {
		TextView bbs_item_title_tv;
		TextView bbs_item_name_tv;
		TextView bbs_item_lookup_tv;
		TextView bbs_item_comment_tv;
		TextView bbs_item_time_tv;
	}
}
